﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.Services;
using System.Web.UI;
using System.Web.UI.WebControls;
using OLTS.DATA_SERVICE;
using OLTS.MODEL;
using OLTS.WEB.BaseClass;

namespace ONTS.WEB
{
    public partial class Default : BaseWebpage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
              //Login();

              if (Page.IsPostBack)
              {
                  Login();
              }
             
        }
        private void Login()
        {
            try
            { 
                var query = Utility.Encrypt(Username.Value);
              
                //var query =  Request.QueryString["id"];
                
                if (query != string.Empty)
                {
                    var user = Utility.Decrypt(query);
                    var cmd = new LOGIN_Service(user);
                    cmd.Execute();

                    Session["LOGIN"] = cmd.Result;

                    if (Session["LOGIN"] != null)
                    {
                        BaseService.LOGIN = (OTS_LOGIN)Session["LOGIN"];
                        BaseService.PROJECT = getProject(BaseService.LOGIN.EMPLOYEE_ID);

                        var model = BaseService.LOGIN;
                        var role  = model.ROLE.Split(':');
                        if (role.Contains("administrator"))
                        {
                            Response.Redirect("Views/EmployeeAll.aspx",false);
                        } 
                        else if (role.Contains("timesheet"))
                        {
                            Response.Redirect("/Views/TimeSheetOverView.aspx", false);
                        }
                        else
                        {
                            Response.Redirect("/Views/AccessDeniedPage.aspx", false);
                        }
                    }
                    else
                    {
                        Response.Redirect("/Views/AccessDeniedPage.aspx", false);
                    }
                }
                else
                {
                    Response.Redirect("/Views/AccessDeniedPage.aspx", false);
                }

            }
            catch (Exception ex)
            {
                ShowAlert(ex.Message);
            }

        }

        [WebMethod]
        public static string SignOut()
        {
            HttpContext.Current.Session["LOGIN"] = null;

            return "SignOut Complete";
        }
        private OTS_PROJECT getProject(string emp_id)
        {
            var cmd = new Project_Service();
            var result = cmd.GetByID(emp_id);
            return result;
        } 

        protected void btnLogin_Click(object sender, EventArgs e)
        {
             
        }
         
    }
}